<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use EloquentFilter\Filterable;
use App\Models\Traits\Admin\ActionButtonTrait;
use App\Services\Mqtt\Subscribes;
use App\Models\Traits\Admin\MqttDataTrait;
use Illuminate\Database\Eloquent\SoftDeletes;

class Machine extends Model
{
    use Filterable, ActionButtonTrait, MqttDataTrait, SoftDeletes;

    protected $appends = [ 'online', 'signal', 'led_state', 'parametersetting', 'version', 'tem_state', 'tem', 'config_data', 'actionButton', 'broker_one', 'broker_two', 'merchant', 'operator'];
    
    protected $actions = ['resetMachine','show','delete'];

    protected $actionUri = 'machines';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'imei', 'sim_no','mainborad_id','state', 'freight_way_demo_id','expire_time','cate','rank', 'is_auto_refund', 'is_cart', 'title','nickname'
    ];   



    public function getBrokerOneAttribute()
    {
       return $this->manager()->where(['parent_id' => 0, 'admin_users.type' => 2])->first();
    }

    public function getBrokerTwoAttribute()
    {
        return $this->manager()->where('admin_users.parent_id', '<>', 0)->where(['admin_users.type' => 2])->first();
    }

    public function getMerchantAttribute()
    {
        return $this->manager()->where('admin_users.parent_id', '<>', 0)->where([ 'admin_users.type' => 3])->first();
    }

    public function getOperatorAttribute()
    {
        return $this->manager()->where('admin_users.parent_id', '<>', 0)->where(['admin_users.type' => 4])->first();
    }

    public function mainborad()
    {
        return $this->belongsTo(Mainborad::class, 'mainborad_id', 'id');
    }

    public function freight_way()
    {
        return $this->hasMany(FreightWay::class, 'machine_id', 'id');
    }

    public function config()
    {
        return $this->hasMany(MachineConfig::class, 'machine_id', 'id');
    }

    public function freight_way_demo()
    {
        return $this->belongsTo(FreightWayDemo::class, 'freight_way_demo_id', 'id');
    }

    public function machine_managers()
    {
        return $this->hasMany(MachineManager::class, 'machine_id', 'id');
    }

    public function manager()
    {
        return $this->belongsToMany(Auth::class, 'machine_managers', 'machine_id', 'manager_id');
    }

    //所有商户
    public function merchant()
    {
        return $this->belongsToMany(Auth::class, 'machine_managers', 'machine_id', 'manager_id')->wherePivot('type', 3);
    }
    //平台
    public function system()
    {
        return $this->belongsToMany(Auth::class, 'machine_managers', 'manager_id', 'machine_id')->wherePivot('type', 1);
    }
    //经销商
    public function broker()
    {
        return $this->belongsToMany(Auth::class, 'machine_managers', 'manager_id', 'machine_id')->wherePivot('type', 2);
    }

    public function getOnlineAttribute()
    {
        $state = 0; 
        if($this->connected_at){//在线
            $time = (int)get_redis_mqtt_state('Time', $this->imei);
            if($time){
                if(time() - get_redis_mqtt_state('Time', $this->imei) >= 450){
                    $this->connected_at = null;
                    $this->disconnected_at = date("Y-m-d H:i:s",time());
                    $this->save();
                    return 2;
                };
            }            
            return 1;
        }
        if($this->disconnected_at){//不在线
            $state = 2;
        }        
        return $state;       
    }

    public function getConfigDataAttribute()
    {
        $config = $this->config;
        foreach ($config as $key => $value) {
            switch ($value->key) {
                case 'heat':
                    $setting_data['temkey'] = 'heat';
                    $setting_data['temtitle'] = '';
                    $setting_data['temvalue'] = $value->value;
                    $setting_data['temstart'] = $value->start;
                    $setting_data['temend'] = $value->end;
                    $setting_data['temid'] = $value->id;
                    $setting_data['temstate'] = $value->state;
                    break;
                case 'cold':
                    $setting_data['temkey'] = 'cold';
                    $setting_data['temtitle'] = '';
                    $setting_data['temvalue'] = $value->value;
                    $setting_data['temstart'] = $value->start;
                    $setting_data['temend'] = $value->end;
                    $setting_data['temid'] = $value->id;
                    $setting_data['temstate'] = $value->state;
                    break;
                case 'led':
                    $setting_data['ledkey'] = 'led';
                    $setting_data['ledstart'] = $value->start;
                    $setting_data['ledend'] = $value->end;
                    $setting_data['ledtitle'] = ''; 
                    $setting_data['ledid'] = $value->id;
                    $setting_data['ledstate'] = $value->state;
                    break;
            }
        }
        return $setting_data;
    }
}
